package lancooUpLoad;

import android.app.Activity;
import android.os.Handler;
import android.util.Log;

import com.example.lancooznbk.presenter.presenter_interface.IBaseReviewPresenter;
import com.example.lancooznbk.view.base.ILoadPaperView;
import com.lancoo.reviewsystem.utils.ToastUtils;
import com.lancoo.rxretrofitlibrary.Api.UserInfo;
import com.lancoo.rxretrofitlibrary.http.HttpManager;
import com.lancoo.rxretrofitlibrary.listener.HttpOnNextListener;

import testPaperWrong.WrongQuesPaperActivity;

/**
 * Created by Administrator on 2017/9/5/005.
 */

public class SaveWrongPresenter implements IBaseReviewPresenter {

    private ILoadPaperView view;
    private int ModelFrom;
    private boolean isFromExit = false;

    public SaveWrongPresenter(ILoadPaperView view, int modelFrom) {
        this.view = (ILoadPaperView) view;
        this.ModelFrom = modelFrom;
    }

    @Override
    public void getData(boolean isShowLoading) {
        //  if (isShowLoading) view.showLoading();
        HttpOnNextListener<SaveAnswerEntityResult> httpOnNextListener = new HttpOnNextListener<SaveAnswerEntityResult>() {

            @Override
            public void onNext(SaveAnswerEntityResult saveAnswerEntityResult) {

                if(saveAnswerEntityResult.getValue().equals("1")){

                    if (isFromExit){
//                        ToastUtils.showShort("数据保存成功");
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {

                                ((Activity)view).finish();
                            }
                        },600);

                    }
                }else{
//                    ToastUtils.showShort("数据保存失败! 错误码"+saveAnswerEntityResult.getValue());
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                //view.showNetError();
                ToastUtils.showShort("数据保存失败!");
            }
        };

        SaveWrongParam api = new SaveWrongParam(httpOnNextListener, (WrongQuesPaperActivity)view);

        Log.e("=====AnsweredCount====","==111=="+PaperInfor.QuesAnsweredItemCount);

        api.setSaveDataWrongJson(DataPackage.getSaveDataWrongJson(UserInfo.StuId,UserInfo.SubjectId,UserInfo.StageNo,PaperInfor.QuesID,PaperInfor.State,
                PaperInfor.Score,PaperInfor.IsAssess,PaperInfor.QuesBrief,PaperInfor.QuesTotalItemCount,PaperInfor.QuesFrom,PaperInfor.TypeID,
                PaperInfor.ImportantZSDCode,PaperInfor.MainZSDCode,PaperInfor.ImportantZSD,PaperInfor.MainZSD,PaperInfor.Difficulty,PaperInfor.TestProbablity,
                PaperInfor.StuName,"",PaperInfor.StuAnswerNewAll,PaperInfor.StuAssessNewAll,PaperInfor.QuesDuration,PaperInfor.CorrectRate,
                PaperInfor.QuesAnsweredItemCount,PaperInfor.PrimitiveQuesID,PaperInfor.PrimQuesItemCount));

        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(api);
    }

    @Override
    public void getMoreData() {
        getData(false);
    }

    @Override
    public void getDataByStatus(int position) {
        getData(false);
    }

    public void getData(boolean isShowing ,boolean isFromExit){
        this.isFromExit = isFromExit;
        getData(isShowing);
    }

}